import Vue from 'vue'
import VueRouter from 'vue-router'




const Login = () => import('../components/login')
const Home = () => import('../components/home')
const WelCome = () => import('../components/welcome')
const Users = () => import('../components/users/users')
const Roles = () => import('../components/power/roles')
const Rights = () => import('../components/power/rights')
const Cate = () => import('../components/goods/Cate')
const Params = () => import('../components/goods/params')
const GoodsList = () => import('../components/goods/list')
const AddPage = () => import('../components/goods/add')
const Order = () => import('../components/order/order')
const Report = () => import('../components/report/report')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: WelCome
      },
      {
        path: '/users',
        component: Users
      }, 
      {
        path: '/roles',
        component: Roles
      },  
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/categories',
        component: Cate
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: GoodsList
      },
      {
        path: '/goods/add',
        component: AddPage
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Report
      },
    ]

    
  }
]
 

const router = new VueRouter({
  routes
})  

// 挂载导航守卫
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  // next('/login')强制跳转  next() 放行
  if (!tokenStr) return next('/login')
   next()

})

   



export default router
